/**
 * 系统字典类型管理初始化
 */
var Dictype = {
    id: "DictypeTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Dictype.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '代码', field: 'code', align: 'center', valign: 'middle', sortable: true},
        {title: '描述', field: 'description', align: 'center', valign: 'middle', sortable: true},
        {title: '产品线', field: 'product.description', align: 'center', valign: 'middle', sortable: true},
        {title: '是否有效', field: 'isActive', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index) {
            return value?"是":"否";
        }}
    ];
};

/**
 * 检查是否选中
 */
Dictype.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Dictype.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增系统字典类型
 */
Dictype.add = function () {
    var index = Sys.open('新增系统字典类型',Sys.backendPath + '/cdc/dictype/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看系统字典类型详情
 */
Dictype.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('系统字典类型详情',Sys.backendPath + '/cdc/dictype/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除系统字典类型
 */
Dictype.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/cdc/dictype/delete", function (data) {
	            Sys.success("删除成功!");
	            Dictype.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("dictypeId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除系统字典类型?",operation);
    }
};

/**
 * 查询系统字典类型列表
 */
Dictype.search = function (id, value) {
    var queryData = {};
    queryData['description'] = value;
    queryData['productId'] = $("#productId").combobox("getValues")[0];
    Dictype.table.refresh({query: queryData});
};
Dictype.ClickRow = function(r,e,f) {
    Dictionary.dictypeId=r.id;
    Dictionary.table.clear();
    Dictionary.table.refresh({query: {dictypeId:r.id}});
}
$(function () {
    var defaultColunms = Dictype.initColumn();
    var table = new BSTable(Dictype.id, Sys.backendPath + "/cdc/dictype/list", defaultColunms);
    table.setPaginationType("server");
    table.setShowColumns(false);
    table.setShowToggle(false);
    table.setShowRefresh(false);
    table.setPageSize(20);
    table.setOnClickRow(Dictype.ClickRow);
    Dictype.table = table.init();
});
